package com.ruoyi.system.service.impl;


import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IPanelGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PanelGroupServiceImpl implements IPanelGroupService {

    @Autowired
    private ChatLogMapper chatLogMapper;

    @Autowired
    private CustomerUserMapper customerUserMapper;

    @Autowired
    private CustomerChatMapper customerChatMapper;

    @Autowired
    private CustomerInfoMapper waUserMapper;
    @Autowired
    private WaClientMapper waClientMapper;

    @Autowired
    private RedisCache redisCache;


    @Override
    public PanelTotal getPanelTotal(List<WaUser> list11) {
        PanelTotal panelTotal = new PanelTotal();

        CustomerChat customerChat = new CustomerChat();
        customerChat.setCustomerNumber(null);
        customerChat.setCreateTime(null);
        List<CustomerChat> list1 = waClientMapper.selectSendTotal(customerChat);
        List<CustomerChat> list3 = waClientMapper.selectClientTotal(customerChat);
        Integer sendTotal = list1.size();
        Integer clientTotal  = list3.size();
        panelTotal.setClientTotal(clientTotal);
        panelTotal.setSendTotal(sendTotal);
        return panelTotal;
    }

    @Override
    public PanelTotal getTodayPanelTotal(List<WaUser> list) {
        PanelTotal panelTotal = new PanelTotal();

//        CustomerChat customerChat1 = new CustomerChat();
//        customerChat1.setCustomerNumber(null);
//        customerChat1.setCreateTime(null);
//        List<CustomerChat> list111 = waClientMapper.selectSendTotal(customerChat1);
//        list111.parallelStream().forEach(user -> {
//            Object objext = redisCache.getCacheObject(user.getClientNumber() + "-send_message");
//            if (StringUtils.isNotNull(objext)) {
//                String status = String.valueOf(objext);
//                user.setStatus(Integer.valueOf(status));
//                waClientMapper.updateCustomerChat(user);
//            }
//        });

        CustomerChat customerChat = new CustomerChat();
        customerChat.setCustomerNumber(null);
        customerChat.setCreateTime(null);
//        List<CustomerChat> list1 = waClientMapper.selectSendTotal(customerChat);
//        List<CustomerChat> list3 = waClientMapper.selectClientTotal(customerChat);
        customerChat.setCreateTime(new Date());
        List<CustomerChat> list2 = waClientMapper.selectToDaySendTotal(customerChat);
        List<CustomerChat> list4 = waClientMapper.selectToDayClientTotal(customerChat);

//        Integer sendTotal = list1.size();
        Integer toDaySendTotal  = list2.size();
//        Integer clientTotal  = list3.size();
        Integer toDayClientTotal  = list4.size();

//        panelTotal.setClientTotal(clientTotal);
//        panelTotal.setSendTotal(sendTotal);


//        panelTotal.setToDayReplyTotal(toDayReplyTotal);
        panelTotal.setToDaySendTotal(toDaySendTotal);
//        panelTotal.setToDayCusTotal(toDayCusTotal);
        panelTotal.setToDayClientTotal(toDayClientTotal);

//        float replyRate = (float) toDayReplyTotal / toDaySendTotal;
//        panelTotal.setToDayReplyRate(replyRate);

        return panelTotal;
    }

    @Override
    public List<UserConversionsTotal> getUserConversionsTotal(List<WaUser> list) {
        List<UserConversionsTotal> totalList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            WaUser waUser = list.get(i);
            UserConversionsTotal conversionsTotal = new UserConversionsTotal();
            conversionsTotal.setUserName(waUser.getUserName());
            conversionsTotal.setContactNumber(waUser.getContactNumber());
            conversionsTotal.setClientCount(waUser.getCustomerChatList().size());
            ChatLog chatLog = new ChatLog();
            chatLog.setCustomerNumber(waUser.getContactNumber());

            chatLog.setUserType("0");
            chatLog.setCreateTime(null);
            List<ChatLog> loglist0 = chatLogMapper.selectChatLogByUserType(chatLog);
            conversionsTotal.setSendCount(loglist0.size());

            chatLog.setCreateTime(new Date());
            List<ChatLog> tlist = chatLogMapper.selectChatLogByUserType(chatLog);
            conversionsTotal.setToDaySendCount(tlist.size());


            chatLog.setUserType("1");
            chatLog.setCreateTime(null);
            List<ChatLog> loglist1 = chatLogMapper.selectChatLogByUserType(chatLog);
            conversionsTotal.setReplyCount(loglist1.size());

            chatLog.setCreateTime(new Date());
            List<ChatLog> tloglist1 = chatLogMapper.selectChatLogByUserType(chatLog);
            conversionsTotal.setToDayReplyCount(tloglist1.size());


            CustomerChat chat = new CustomerChat();
            chat.setCustomerNumber(waUser.getContactNumber());
            chat.setCreateTime(null);
            List<CustomerChat> customerList = customerChatMapper.selectConversionsByCode(chat);
            conversionsTotal.setConversions(customerList.size());

            chat.setCreateTime(new Date());
            List<CustomerChat> toDayList = customerChatMapper.selectConversionsByCode(chat);
            conversionsTotal.setToDayconversions(toDayList.size());


            totalList.add(conversionsTotal);
        }
        // 添加排序功能
        Collections.sort(totalList, (o1, o2) -> Integer.compare(o2.getClientCount(), o1.getClientCount()));
        return totalList;
    }
}
